package com.studyyoun.android.nonghe.home.item;
/**
 * Created by zhaolong on 2017/10/14.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.studyyoun.android.androidcommonbaselibrary.execut.ExecutorFunction;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.nonghe.home.adapter.JpushMsgAdapter;
import com.studyyoun.android.nonghe.push.JPushReceiverModel;
import com.studyyoun.android.nonghe.push.sql.PushReceiverSqlDBHelper;

import java.util.List;

/**
 * class infation
 */
public class ItemJpushMsg {
    private PushReceiverSqlDBHelper mPushReceiverSqlDBHelper;

    private RecyclerView mRecyclerView;

    private Context mContext;
    private Handler mHandler;
    private JpushMsgAdapter mJpushMsgAdapter;
    private final LinearLayoutManager mLinearLayoutManager;

    public ItemJpushMsg(Context context, RecyclerView jpushRecyclerView, Handler handler) {

        mPushReceiverSqlDBHelper = new PushReceiverSqlDBHelper(context);
        mHandler = handler;
        mContext = context;
        mRecyclerView = jpushRecyclerView;
        mLinearLayoutManager = new LinearLayoutManager(context);
        jpushRecyclerView.setLayoutManager(mLinearLayoutManager);

        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (newState==0){
                    LogUtils.d("开始滑动");
                    mCurrentIndex=mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
                    mHandler.removeCallbacks(mRunnable);
                }else if (newState==2){
                    LogUtils.d("结束滑动");
                    mHandler.postDelayed(mRunnable,1000);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }


    private int mCurrentIndex = 0;

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {

            if (mCurrentIndex > mLinearLayoutManager.getItemCount() - 1) {
                mCurrentIndex = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
                mRecyclerView.scrollToPosition(0);
            }else {
                mCurrentIndex++;
                mRecyclerView.scrollToPosition(mCurrentIndex);
                mHandler.postDelayed(mRunnable, 2000);
            }
        }
    };

    public void loadingLocationDatas() {
        ExecutorFunction.getInstance().addRunnableTask(new Runnable() {
            @Override
            public void run() {
                final List<JPushReceiverModel> jPushReceiverModels = mPushReceiverSqlDBHelper.queryAllMsgModelList();

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (jPushReceiverModels == null || jPushReceiverModels.size() == 0) {
                            mRecyclerView.setVisibility(View.GONE);
                        } else {
                            mRecyclerView.setVisibility(View.VISIBLE);
                            setJpushDataShow(jPushReceiverModels);
                        }
                    }
                });

            }
        });
    }

    private void setJpushDataShow(List<JPushReceiverModel> jPushReceiverModels) {


        if (mJpushMsgAdapter == null) {
            mJpushMsgAdapter = new JpushMsgAdapter(mContext, jPushReceiverModels);
            mRecyclerView.setAdapter(mJpushMsgAdapter);
        } else {
            mJpushMsgAdapter.setListDatas(jPushReceiverModels);
            mJpushMsgAdapter.notifyDataSetChanged();
        }

        if (jPushReceiverModels.size() > 0) {
            mHandler.removeCallbacks(mRunnable);
            mCurrentIndex = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
            mHandler.post(mRunnable);
        }

    }

    public void stopMsgFunction() {
        mHandler.removeCallbacks(mRunnable);
    }

    public void startMsgFunction() {
        if (mLinearLayoutManager.getItemCount() != 0) {
            mHandler.post(mRunnable);
        }
    }

}
